// 统计数据加载
async function loadStatsData() {
    try {
        // 分别获取各模块数据
        const [newsRes, typesRes, usersRes, commentsRes] = await Promise.all([
            fetch('./mock/news.json'),
            fetch('./mock/types.json'),
            fetch('./mock/users.json'),
            fetch('./mock/comments.json')
        ]);
        
        const newsList = await newsRes.json();
        const typesList = await typesRes.json();
        const usersList = await usersRes.json();
        const commentsList = await commentsRes.json();

        // 更新统计卡片数据
        document.getElementById('total-news').textContent = newsList.length;
        document.getElementById('total-types').textContent = typesList.length;
        document.getElementById('total-users').textContent = usersList.length;
        document.getElementById('total-comments').textContent = commentsList.length;

        // 加载近期新闻列表（按时间倒序取前5条）
        const sortedNews = [...newsList].sort((a, b) => 
            new Date(b.time) - new Date(a.time)
        ).slice(0, 5);
        renderRecentNews(sortedNews);

    } catch (error) {
        console.error('加载统计数据失败：', error);
        // 降级显示默认数据
        document.getElementById('total-news').textContent = '4';
        document.getElementById('total-types').textContent = '4';
        document.getElementById('total-users').textContent = '2';
        document.getElementById('total-comments').textContent = '6';
        
        // 渲染默认近期新闻
        renderRecentNews([
            { id: 1, title: '国内经济稳步增长', type: '国内', time: '2025-10-01', status: '已发布' },
            { id: 2, title: '全球气候峰会达成新共识', type: '国际', time: '2025-09-30', status: '已发布' },
            { id: 3, title: '新型AI模型预测天气', type: '科技', time: '2025-09-29', status: '已发布' },
            { id: 4, title: '某电影国庆档票房破10亿', type: '娱乐', time: '2025-10-02', status: '已发布' }
        ]);
    }
}

// 渲染近期新闻表格
function renderRecentNews(newsList) {
    const tableBody = document.querySelector('#recent-news-table tbody');
    tableBody.innerHTML = '';

    newsList.forEach(news => {
        const row = document.createElement('tr');
        row.innerHTML = `
            <td>${news.id}</td>
            <td>${news.title}</td>
            <td>${news.type}</td>
            <td>${news.time}</td>
            <td>${news.status}</td>
        `;
        tableBody.appendChild(row);
    });
}

// 页面加载初始化
window.onload = loadStatsData;